package com.skillworld.webapp.model.util;

import com.skillworld.webapp.model.award.Award;
import com.skillworld.webapp.model.award.AwardDao;
import com.skillworld.webapp.model.tutorial.Tutorial;
import com.skillworld.webapp.model.user.User;

import es.udc.pojo.modelutil.exceptions.InstanceNotFoundException;

public class AssignAward {

    private final static long AWARD_ID_OF_N_TUTS_CREATED = 1L;
    private final static long AWARD_ID_OF_N_TUTS_VALIDATED = 2L;
    private final static long AWARD_ID_OF_PREMIER_TUT_ACCEPTED = 3L;
    private final static long AWARD_ID_OF_N_STUDENTS = 4L;
    private final static long AWARD_ID_OF_N_COURSES_CREATED = 5L;
    private final static long AWARD_ID_OF_PREMIER_COURSE_DONE = 6L;
    private final static long AWARD_ID_OF_COURSES_DONE_IN_ALL_DEPTS = 7L;
    private final static long AWARD_ID_OF_N_COURSES_DONE = 8L;
    
    private final static int NUM_TUT_CREATED_TO_ADD_AWARD = 100;
    private final static int NUM_TUT_VALIDATED_TO_ADD_AWARD = 100;
    
    private final static int NUM_STUDENTS_TO_ADD_AWARD = 100;
    private final static int NUM_COURSES_CREATED_TO_ADD_AWARD = 10;
    private final static int NUM_COURSES_DONE_TO_ADD_AWARD = 100;
    
    private AwardDao awardDao;
    private Tutorial tutorial;
    private User user;
    
    public AssignAward(Tutorial tutorial, AwardDao awardDao) {
        this.tutorial = tutorial;
        this.awardDao = awardDao;
    }
    
    public AssignAward(User user, AwardDao awardDao){
        this.user = user;
        this.awardDao = awardDao;
    }
    
    //Tutorials
    public void addAwardByValidation(){
        this.addUserAwardByCreatedTuts();
        this.addUserAwardByValidatedTuts();
        this.addUserAwardByPrimerTutAceite();
    }
    
    public void addUserAwardByCreatedTuts(){
        int numberTuts = getAuthor().getDerived().getAcceptedTutorials();
        
        if(numberTuts == NUM_TUT_CREATED_TO_ADD_AWARD){
            
            try {
                Award award = awardDao.findById(AWARD_ID_OF_N_TUTS_CREATED);
                getAuthor().addAward(award);
            } catch (InstanceNotFoundException e) {}
        }
    }
    
    public void addUserAwardByValidatedTuts(){
        int numberTuts = getValidator().getDerived().getValidatedTutorials();
        
        if(numberTuts == NUM_TUT_VALIDATED_TO_ADD_AWARD){
           
            try {
                Award award = awardDao.findById(AWARD_ID_OF_N_TUTS_VALIDATED);
                getValidator().addAward(award);
            } catch (InstanceNotFoundException e) {}
        }
    }
    
    public void addUserAwardByPrimerTutAceite(){
        int numberTuts = getAuthor().getDerived().getAcceptedTutorials();
        
        if(numberTuts == 1){
             
            try {
                Award award = awardDao.findById(AWARD_ID_OF_PREMIER_TUT_ACCEPTED);
                getAuthor().addAward(award);
            } catch (InstanceNotFoundException e) {}
        }
    }
    
    //Courses
    //MC
    public void addUserAwardByNumStudents(){
        int numbersStudents = user.getDerived().getStudents();
        
        if(numbersStudents == NUM_STUDENTS_TO_ADD_AWARD){
             
            try {
                Award award = awardDao.findById(AWARD_ID_OF_N_STUDENTS);
                user.addAward(award);
            } catch (InstanceNotFoundException e) {}
        }
    }
    
    public void addUserAwardByNumCousesCreated(){
        int numberCouses = user.getDerived().getCreatedCourses();
        
        if(numberCouses == NUM_COURSES_CREATED_TO_ADD_AWARD){
             
            try {
                Award award = awardDao.findById(AWARD_ID_OF_N_COURSES_CREATED);
                user.addAward(award);
            } catch (InstanceNotFoundException e) {}
        }
    }
    
    //User
    public void addUserAwardByCousesDone(){
        this.addUserAwardByPrimerCousesDone();
        this.addUserAwardByNCousesDone();
        this.addUserAwardByCousesDoneInAllDepts();
    }
    
    protected void addUserAwardByPrimerCousesDone(){
        int numberCouses = user.getDerived().getCoursesDone();
        
        if(numberCouses == 1){
             
            try {
                Award award = awardDao.findById(AWARD_ID_OF_PREMIER_COURSE_DONE);
                user.addAward(award);
            } catch (InstanceNotFoundException e) {}
        }
    }

    protected void addUserAwardByNCousesDone(){
        int numberCouses = user.getDerived().getCoursesDone();
        
        if(numberCouses == NUM_COURSES_DONE_TO_ADD_AWARD){
             
            try {
                Award award = awardDao.findById(AWARD_ID_OF_N_COURSES_DONE);
                user.addAward(award);
            } catch (InstanceNotFoundException e) {}
        }
    }
    
    protected void addUserAwardByCousesDoneInAllDepts(){
        boolean lable = user.getDerived().getCoursesInAllDepts();
        
        if(lable == true){
             
            try {
                Award award = awardDao.findById(AWARD_ID_OF_COURSES_DONE_IN_ALL_DEPTS);
                user.addAward(award);
            } catch (InstanceNotFoundException e) {}
        }
    }
    
    protected User getAuthor(){
        return tutorial.getAuthor();
    }
    
    protected User getValidator(){
        return tutorial.getValidator();
    }
}
